package com.test.pay;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ijpay.core.kit.PayKit;
import com.xingluo.pay.core.wx.model.WxRefundOrderRequest;
import com.xingluo.pay.core.wx.model.WxUnifiedOrderRequest;
import com.xingluo.pay.core.wx.service.WxPayV3Service;
import com.xingluo.tools.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Slf4j
@Tag(name="支付接口")
@RestController
@RequestMapping("/pay")
public class WxV3PayController {

    @Resource
    private WxPayV3Service wxPayV3Service;

    @Operation(summary = "获取证书序列号", description = "获取证书序列号")
    @GetMapping("/serial")
    public Result serial(){
        return Result.success(wxPayV3Service.getSerialNumber());
    }

    @Operation(summary = "获取平台证书并下载", description = "获取平台证书并下载")
    @GetMapping("/platformCert")
    public Result platformCert(){
        return Result.success(wxPayV3Service.getPlatformCerts());
    }

    @Operation(summary = "获取平台证书序列号", description = "获取平台证书序列号")
    @GetMapping("/platformSerial")
    public Result platformSerial(){
        return Result.success(wxPayV3Service.getPlatSerialNumber());
    }

    @Operation(summary = "统一下单", description = "统一下单")
    @GetMapping("/unifiedOrder")
    public Result unifiedOrder(@RequestParam(name="type", defaultValue = "NATIVE") String type){
        WxUnifiedOrderRequest unifiedOrderRequest = new WxUnifiedOrderRequest()
                .setType(type)
                .setDesc("测试下单")
                .setAttach("id:2")
                .setOutTradeNo(PayKit.generateStr())
                .setTotalFen(1);
        Map<String, String> map = wxPayV3Service.unifiedOrder(unifiedOrderRequest);
        return Result.success(map);
    }

    @Operation(summary = "支付通知", description = "支付通知")
    @GetMapping("/payNotify")
    public void payNotify(HttpServletRequest request, HttpServletResponse response){
        wxPayV3Service.wxPayNotify(request, response, orderInfo -> {
            // 支付成功处理，orderInfo是支付订单对象json
            System.out.println(orderInfo);
            // 返回参数可参考 https://pay.weixin.qq.com/docs/merchant/apis/jsapi-payment/payment-notice.html
            JSONObject jsonObject = JSONUtil.parseObj(orderInfo);
            System.out.println(jsonObject.getStr("trade_state"));
            System.out.println(jsonObject.getStr("out_trade_no"));
            System.out.println(jsonObject.getStr("attach"));
        },orderInfo->{
            // 支付失败处理，orderInfo是支付订单对象json
            System.out.println(orderInfo);
            JSONObject jsonObject = JSONUtil.parseObj(orderInfo);
            System.out.println(jsonObject.getStr("trade_state"));
            System.out.println(jsonObject.getStr("out_trade_no"));
        });
    }

    @Operation(summary = "查询订单", description = "查询订单")
    @GetMapping("/queryOrder")
    public Result queryOrder(){
        String outTradeNo = "";
        // 返回参数可参考 https://pay.weixin.qq.com/docs/merchant/apis/jsapi-payment/query-by-out-trade-no.html
        return Result.success(wxPayV3Service.queryOrder(outTradeNo));
    }

    @Operation(summary = "退款申请", description = "退款申请")
    @GetMapping("/refund")
    public Result refund(){
        WxRefundOrderRequest refundOrderRequest = new WxRefundOrderRequest()
                .setOutRefundNo(PayKit.generateStr())
                .setOutTradeNo("")
                .setRefundFen(1)
                .setTotalFen(1)
                .setReason("测试退款");
        // 返回参数可参考 https://pay.weixin.qq.com/docs/merchant/apis/jsapi-payment/create.html
        return Result.success(wxPayV3Service.refund(refundOrderRequest));
    }

    @Operation(summary = "退款通知", description = "退款通知")
    @GetMapping("/refundNotify")
    public void refundNotify(HttpServletRequest request, HttpServletResponse response){
        wxPayV3Service.wxPayNotify(request, response, orderInfo -> {
            // 退款成功处理，orderInfo是支付订单对象json
            System.out.println(orderInfo);
            // 返回参数可参考 https://pay.weixin.qq.com/docs/merchant/apis/jsapi-payment/refund-result-notice.html
            JSONObject jsonObject = JSONUtil.parseObj(orderInfo);
            System.out.println(jsonObject.getStr("refund_status"));
            System.out.println(jsonObject.getStr("out_trade_no"));
            System.out.println(jsonObject.getStr("out_refund_no"));
        },orderInfo->{
            // 退款失败处理，orderInfo是支付订单对象json
            System.out.println(orderInfo);
            JSONObject jsonObject = JSONUtil.parseObj(orderInfo);
            System.out.println(jsonObject.getStr("refund_status"));
            System.out.println(jsonObject.getStr("out_trade_no"));
            System.out.println(jsonObject.getStr("out_refund_no"));
        });
    }

}
